package com.logic.landseaserver.ws;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.logic.landseaserver.common.LandeaConstants;
import com.logic.landseaserver.common.exception.LandseaException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.exception.BusinessException;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.landseaserver.service.ICheckFeeListService;
import com.logic.landseaserver.ws.dto.CheckFeeListDTO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @Author: jack
 * @Date: 2017/6/16
 * @Description: 交割单
 */
@Path("/checkFeeList")
@Api(value="checkFeeList")
public class CheckFeeListRest {

	@Autowired
	private ICheckFeeListService iCheckFeeListService;
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
    //查询详情
	@GET
    @Path("/{contractId}")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Get checkFeeListId detail ", position = 4)
    public Response getCheckListById(
            @ApiParam(value = "param", required = true) @PathParam("contractId") Integer contractId) {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData(iCheckFeeListService.getCheckFeeListDitail(contractId));
        } catch (Exception e) {
            logger.error("getCheckListById error", e);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
	

	//增加
	@POST
	@Path("/createCheckFeeList")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "Create a new createCheckFeeList",position = 1)
	public Response createCheckFeeList(@ApiParam(value = "param", required = true) CheckFeeListDTO checkFeeListDTO) {
		CommonResultMap map = new CommonResultMap();
        try {
        	int id = iCheckFeeListService.insertCheckFeeList(checkFeeListDTO);
        	checkFeeListDTO.setId(id);
            map.setData(checkFeeListDTO);
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (BusinessException e) {
            logger.error("createCheckFeeList error", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("createCheckFeeList error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}
    //编辑
    @PUT
	@Path("/editCheckFeeList")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "Edit CheckFeeList ")
    public Response editCheckList(@ApiParam(value = "param", required = true) CheckFeeListDTO checkFeeListDTO) {
        CommonResultMap map = new CommonResultMap();
        try {
        	iCheckFeeListService.editCheckFeeList(checkFeeListDTO);
            map.setResult(CommonResult.SUCCESS,"edit CheckFeeListDTO");
        } catch (Exception e) {
            logger.error("editCheckList error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @GET
    @Path("/queryEndAndLast/{contractId}")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "退房时结算单中显示退房交割水电以及上次水电",position = 4)
    public Response queryEndAndLastCheckFeeList(
                @ApiParam(value = "param", required = true) @PathParam("contractId") Integer contractId) {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData(iCheckFeeListService.queryEndAndLastFees(contractId));
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (LandseaException e) {
            logger.error("queryEndAndLast error", e);
            map.setResult(CommonResult.ERROR, e.getErrorDesc());
        } catch (Exception e) {
            logger.error("queryEndAndLast error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/synFeeList/{contractId}")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "同步A合同退房交割单至B合同入住单，传参为B合同ID")
    public Response synFeeList(
            @ApiParam(value = "param", required = true) @PathParam(value = "contractId") Integer contractId) {
        CommonResultMap map = new CommonResultMap();
        try {
            iCheckFeeListService.synFeeList(contractId);
            map.setResult(CommonResult.SUCCESS);
        }catch (LandseaException e) {
            logger.error("offRent || createPreBill error.", e);
            map.setResult(CommonResult.ERROR, e.getErrorDesc());
        } catch (Exception e) {
            logger.error("offRent || createPreBill error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/sysFeeListToExpenses/{contractId}")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "同步A合同退房交割单运营数据，传参为B合同ID")
    public Response sysFeeListToExpenses(
            @ApiParam(value = "param", required = true) @PathParam(value = "contractId") Integer contractId) {
        CommonResultMap map = new CommonResultMap();
        try {
            iCheckFeeListService.sysFeeListToExpenses(contractId, LandeaConstants.CheckType.UN_RENT);
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error(" sysFeeListToExpenses error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

}
